package com.tjtt.gwm.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.gwm.bean.Guzhangku;
import com.tjtt.gwm.bean.Weituodan;
import com.tjtt.gwm.dao.WeituodanDao;
import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;

public class WeituodanDaoImpl extends BaseHibernateDao implements WeituodanDao {

	/*
	 * 保存委托单
	 * wxj
	 */
	public void saveWeituodan(Weituodan weituodan){
		this.getHibernateTemplate().save(weituodan);
	}
	/*
	 * 根据表名和对象id获取flowtaskId
	 * wxj
	 */
	public List getFlowtaskId(String tablename,String mainid){
		String sql = "select t.id from FLOW_TASK t where t.table_name='"+tablename+"' and t.main_id='"+mainid+"'";
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	/*
	 * 获取FlowTaskInfo对象
	 * wxj
	 */
	public FlowTaskInfo getFlowTaskInfo(String flowTaskId){
		String sql = "select t.id from FLOW_TASK_INFO t where t.flow_task_id="+flowTaskId;
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		if(null != list && list.size()>0){
			String flowtaskinfoId = list.get(0).toString();
			return this.getHibernateTemplate().get(FlowTaskInfo.class, Long.parseLong(flowtaskinfoId));
		}else{
			return null;
		}
		
	}
	/*
	 * 保存FlowTaskInfo对象
	 * wxj
	 */
	public void updateFlowTaskInfo(FlowTaskInfo flowTaskInfo){
		this.getHibernateTemplate().update(flowTaskInfo);
	}
	/*
	 * 根据id获取委托单对象
	 * wxj
	 */
	public Weituodan queryWeituodanByid(String id){
		return this.getHibernateTemplate().get(Weituodan.class, Long.parseLong(id));
	}
	/*
	 * 根据id获取FlowTaskInfo对象
	 * wxj
	 */
	public FlowTaskInfo queryFlowTaskInfo(String id){
		return this.getHibernateTemplate().get(FlowTaskInfo.class, Long.parseLong(id));
	}
	/*
	 * 查询委托单识别号
	 * wxj
	 */
	public List queryWeituodanShibieno(String year){
		String sql = "select max(substr(t.SHIBIEHAO,length(t.SHIBIEHAO)-3,4)) from WEITUODAN_SHIBIEHAO t where  t.SHIBIEHAO like '%"+year+"-%'";
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	/*
	 * 修改委托单
	 * wxj
	 */
	public void updateWeituodan(Weituodan weituodan){
		this.getHibernateTemplate().update(weituodan);
	}
	/*
	 * 获取flowtask对象
	 * wxj
	 */
	public FlowTask queryFlowTask(String id){
		return this.getHibernateTemplate().get(FlowTask.class, Long.parseLong(id));
	}
	/*
	 * 获取流程样品
	 * wxj
	 */
	public List qeuryTaskYangpin(String flowtaskId){
		String sql = "select t.id,t.t_samplecount from GWM_SAMPLE t where t.t_taskid="+flowtaskId;
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	/*
	 * 根据流程id获取项目分配用户
	 * wxj
	 */
	public List queryDeptleaderId(String flowtaskId){
		String sql = "select distinct t.t_deptid,t.t_deptname,t.t_deptleaderid,t.t_deptleadername  from gwm_item_sample t where t.t_taskid="+flowtaskId;
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	/*
	 * 查询样品-检测项目任务列表
	 * wxj
	 */
	public PageResult queryItemSampleByTaskId(Map filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*)  FROM GWM_ITEM_SAMPLE T WHERE 1=1 ");
		queryString.append("select t.id,i.t_jiaceleibie,t.t_itemname,t.t_jiancebiaozhun,t_sampleno,t.T_JIANCEYUANID,t.T_JIANCEYUANNAME from GWM_ITEM_SAMPLE t left join GWM_ITEM_FLOW i on t.T_ITEMID = i.id where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			
			queryCountString.append("and T.T_TASKID ='" + filterMap.get("flowtaskid")+"'");
			queryString.append("and T.T_TASKID ='" + filterMap.get("flowtaskid")+"'");
			if(null!=filterMap.get("userid") && !"".equals(filterMap.get("userid"))){
				queryCountString.append("and T.T_JIANCEYUANID ='" + filterMap.get("userid")+"'");
				queryString.append("and T.T_JIANCEYUANID ='" + filterMap.get("userid")+"'");
			}
			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		} 
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	/*
	 * 保存对象
	 * wxj
	 */
	public void saveObj(Object obj){
		this.getHibernateTemplate().save(obj);
	}
	/*
	 * 故障信息
	 * wxj
	 */
	public PageResult queryGuzhangByTaskId(Map filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*)  FROM GUZHANGKU T WHERE 1=1 ");
		queryString.append("select t.id,t.CHXINGNAME,t.XITONGNAME,t.LINGBUJIANNAME,t.GUZHANGPATTERNNAME,t.GUZHANG_DENGJI,decode((select count(*) from TDM_ATTACHMENT a where a.code_id=t.id ),'0','否','是') as GUZHANGPIC,t.GUZHANG_MIAOSHU,t.TASKID,t.licheng,t.GUZHANGFASHENGTIME,t.ADDUSER,t.SHIBIEHAO from GUZHANGKU t where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null!=filterMap.get("flowtaskid") && !"".equals(filterMap.get("flowtaskid"))){
				queryCountString.append("and T.TASKID ='" + filterMap.get("flowtaskid")+"'");
				queryString.append("and T.TASKID ='" + filterMap.get("flowtaskid")+"'");
			}
			if(null!=filterMap.get("CHEXING") && !"".equals(filterMap.get("CHEXING"))){
				queryCountString.append("and T.CHEXING ='" + filterMap.get("CHEXING")+"'");
				queryString.append("and T.CHEXING ='" + filterMap.get("CHEXING")+"'");
			}
			if(null!=filterMap.get("ZIXITONG") && !"".equals(filterMap.get("ZIXITONG"))){
				queryCountString.append("and T.ZIXITONG ='" + filterMap.get("ZIXITONG")+"'");
				queryString.append("and T.ZIXITONG ='" + filterMap.get("ZIXITONG")+"'");
			}
			if(null!=filterMap.get("LINGBUJIAN") && !"".equals(filterMap.get("LINGBUJIAN"))){
				queryCountString.append("and T.LINGBUJIAN ='" + filterMap.get("LINGBUJIAN")+"'");
				queryString.append("and T.LINGBUJIAN ='" + filterMap.get("LINGBUJIAN")+"'");
			}
			
			if(null!=filterMap.get("SHIBIEHAO") && !"".equals(filterMap.get("SHIBIEHAO"))){
				queryCountString.append("and T.SHIBIEHAO like '%" + filterMap.get("SHIBIEHAO")+"%'");
				queryString.append("and T.SHIBIEHAO like '%" + filterMap.get("SHIBIEHAO")+"%'");
			}
			if(null!=filterMap.get("GUZHANGFASHENGTIME") && !"".equals(filterMap.get("GUZHANGFASHENGTIME"))){
				queryCountString.append("and T.GUZHANGFASHENGTIME = '" + filterMap.get("GUZHANGFASHENGTIME")+"'");
				queryString.append("and T.GUZHANGFASHENGTIME = '" + filterMap.get("GUZHANGFASHENGTIME")+"'");
			}
			if(null!=filterMap.get("ADDUSER") && !"".equals(filterMap.get("ADDUSER"))){
				queryCountString.append("and T.ADDUSER like '%" + filterMap.get("ADDUSER")+"%'");
				queryString.append("and T.ADDUSER like '%" + filterMap.get("ADDUSER")+"%'");
			}
			if(null!=filterMap.get("GUZHANGNAME") && !"".equals(filterMap.get("GUZHANGNAME"))){
				queryCountString.append("and T.GUZHANGPATTERNNAME like '%" + filterMap.get("GUZHANGNAME")+"%'");
				queryString.append("and T.GUZHANGPATTERNNAME like '%" + filterMap.get("GUZHANGNAME")+"%'");
			}
			if(null!=filterMap.get("LICHENG1") && !"".equals(filterMap.get("LICHENG1"))){
				queryCountString.append("and T.LICHENG >= '" + filterMap.get("LICHENG1")+"'");
				queryString.append("and T.LICHENG >= '" + filterMap.get("LICHENG1")+"'");
			}
			if(null!=filterMap.get("LICHENG2") && !"".equals(filterMap.get("LICHENG2"))){
				queryCountString.append("and T.LICHENG <= '" + filterMap.get("LICHENG2")+"'");
				queryString.append("and T.LICHENG <= '" + filterMap.get("LICHENG2")+"'");
			}
			if(null!=filterMap.get("GUZHANGDENGJI") && !"".equals(filterMap.get("GUZHANGDENGJI"))){
				queryCountString.append("and T.GUZHANG_DENGJI = '" + filterMap.get("GUZHANGDENGJI")+"'");
				queryString.append("and T.GUZHANG_DENGJI = '" + filterMap.get("GUZHANGDENGJI")+"'");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		} 
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	/*
	 * 零部件或子系统
	 * wxj
	 */
	public PageResult queryLingbujianAndZixitong(Map filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		if (filterMap!= null && !filterMap.isEmpty()) {
			if( "1".equals(filterMap.get("flag"))){
				queryCountString.append("select count(*)  FROM LINGBUJIAN T WHERE 1=1 ");
				queryString.append("select t.id,t.lingbujianname from LINGBUJIAN t where 1=1 ");
			   if(null != filterMap.get("zixitongId") && !"".equals(filterMap.get("zixitongId"))){
					queryCountString.append("and T.zixitong ='" + filterMap.get("zixitongId")+"'");
					queryString.append("and T.zixitong ='" + filterMap.get("zixitongId")+"'");
			   }
			}else{
				queryCountString.append("select count(*)  FROM XITONG T WHERE 1=1 ");
				queryString.append("select t.id,t.ZIXITONGNAME from XITONG t where 1=1 ");
				if(null != filterMap.get("zixitongId") && !"".equals(filterMap.get("zixitongId"))){
					queryCountString.append("and T.SHIYANFENLEI ='" + filterMap.get("zixitongId")+"'");
					queryString.append("and T.SHIYANFENLEI ='" + filterMap.get("zixitongId")+"'");
				}
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		} 
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	/*
	 * 获取故障对象
	 * wxj
	 */
	public Guzhangku getGuzhangById(String id){
		return this.getHibernateTemplate().get(Guzhangku.class, Long.parseLong(id));
	}
	/*
	 * 查询故障模式列表
	 * wxj
	 */
	public PageResult queryGuzhangmoshi(Map filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*)  from GUZHANGPATTERN t,GUZHANGMOSHI g where g.id=t.suoshumoshi ");
		queryString.append("select t.id,t.guzhangpattern,g.moshiname from GUZHANGPATTERN t,GUZHANGMOSHI g where g.id=t.suoshumoshi ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null!=filterMap.get("guzhangname") && !"".equals(filterMap.get("guzhangname"))){
				queryCountString.append("and T.guzhangpattern like '%" + filterMap.get("guzhangname")+"%'");
				queryString.append("and T.guzhangpattern like '%" + filterMap.get("guzhangname")+"%'");
			}
			if(null!=filterMap.get("guzhangmoshi") && !"".equals(filterMap.get("guzhangmoshi"))){
				queryCountString.append("and g.moshiname like '%" + filterMap.get("guzhangmoshi")+"%'");
				queryString.append("and g.moshiname like '%" + filterMap.get("guzhangmoshi")+"%'");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
}
